package org.fast.develop.logicflow.springboot.configuration;

import org.fast.develop.logicflow.LogicFlowManagementConfiguration;
import org.fast.develop.logicflow.management.service.LogicModelService;
import org.fast.develop.logicflow.management.service.LogicRepositoryService;
import org.fast.develop.logicflow.persistence.LogicJpaConfigratuion;
import org.fast.develop.logicflow.persistence.jpa.repository.LogicBytearrayJpaRepository;
import org.fast.develop.logicflow.persistence.jpa.repository.LogicDefinitionJpaRepository;
import org.fast.develop.logicflow.persistence.jpa.repository.LogicDeploymentJpaRepository;
import org.fast.develop.logicflow.persistence.jpa.repository.LogicModelJpaRepository;
import org.fast.develop.logicflow.rest.execute.LogicFlowExecutorEnhance;
import org.fast.develop.logicflow.runtime.LogicFlowRuntimeConfiguration;
import org.fast.develop.logicflow.runtime.LogicRuntimeService;
import org.fast.develop.logicflow.runtime.cache.IThreadStore;
import org.fast.develop.logicflow.ws.LogicFlowServerEndpoint;
import org.fast.develop.logicflow.ws.thread.ThreadStore;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * @author 莫寅
 * @createTime 2022-11-25 13:03
 * @desc：
 */
@AutoConfigureAfter(LogicFlowRuntimeAutoConfiguration.class)
@Configuration
@Import(
        LogicJpaConfigratuion.class
)
@EnableWebSocket
public class LogicFlowManagementAutoConfiguration {


    @Bean
    public LogicFlowExecutorEnhance logicFlowExecutorEnhance(LogicFlowManagementConfiguration logicFlowManagementConfiguration){
        return logicFlowManagementConfiguration.getLogicFlowExecutorEnhance();
    }

    @Bean
    public ServerEndpointExporter endpointExporter(){
        return new ServerEndpointExporter();
    }


    @Bean
    public LogicFlowServerEndpoint logicFlowServerEndpoint(LogicFlowManagementConfiguration logicFlowManagementConfiguration){
        return logicFlowManagementConfiguration.getLogicFlowServerEndpoint();
    }


    @Bean
    public LogicFlowManagementConfiguration logicFlowManagementConfiguration(LogicFlowRuntimeConfiguration logicFlowRuntimeConfiguration,
                                                                             LogicBytearrayJpaRepository logicBytearrayJpaRepository,
                                                                             LogicDefinitionJpaRepository logicDefinitionJpaRepository,
                                                                             LogicDeploymentJpaRepository logicDeploymentJpaRepository,
                                                                             LogicModelJpaRepository logicModelJpaRepository){
        return new DefaultLogicFlowManagementConfiguration(
                logicFlowRuntimeConfiguration,
                logicBytearrayJpaRepository,
                logicDefinitionJpaRepository,
                logicDeploymentJpaRepository,
                logicModelJpaRepository
        );
    }

    @Bean
    public LogicModelService logicModelService(LogicFlowManagementConfiguration logicFlowManagementConfiguration){
        return logicFlowManagementConfiguration.getLogicModelService();
    }


    @Bean
    public LogicRepositoryService logicRepositoryService(LogicFlowManagementConfiguration logicFlowManagementConfiguration){
        return logicFlowManagementConfiguration.getRepositoryService();
    }

    @Bean
    public LogicRuntimeService logicRuntimeService(LogicFlowManagementConfiguration logicFlowManagementConfiguration){
        return logicFlowManagementConfiguration.getRuntimeService();
    }

    @Bean
    public IThreadStore threadStore(LogicFlowManagementConfiguration logicFlowManagementConfiguration){
        return logicFlowManagementConfiguration.getThreadStore();
    }
}
